package com.snailhd.common.db;

import com.snailhd.common.db.resultparsehander.IntegerForDBResultParseHandler;
import com.snailhd.common.db.resultparsehander.ObjectForDBResultParseHandler;
import com.snailhd.common.db.resultparsehander.StringForDBResultParseHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hd
 * @description
 * @date 2019-08-17 17:58
 */
public class DBResultParseInvoker {
    List<AbstractDBResultParseHandler> simpleResultHandlers = new ArrayList(){{
        add(new StringForDBResultParseHandler());
        add(new IntegerForDBResultParseHandler());
    }};




    public boolean isSimpleResut(Class clazz){
        for(AbstractDBResultParseHandler handler: simpleResultHandlers){
            if(handler.supportsResult(clazz)){
                return true;
            }
        }
        return false;
    }


    public <T> T invoke(ResultSet rs,Class<T> clazz,String field) throws SQLException {
        for(AbstractDBResultParseHandler handler: simpleResultHandlers){
            if(handler.supportsResult(clazz)){
                return handler.handle(rs,clazz,field);
            }
        }
        return null;
    }

    public <T> T invokeObject(ResultSet rs,Class<T> clazz) throws SQLException {
        ObjectForDBResultParseHandler handler = new ObjectForDBResultParseHandler();
        return handler.handle(rs,clazz,null);
    }

}
